package com.edu.test;

import com.edu.bean.Province;
import com.edu.mapper.CityMapper;
import com.edu.mapper.ProvinceMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MyTest02 {

    SqlSession sqlSession1 = null;

    SqlSession sqlSession2 = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession1 = sessionFactory.openSession();
        sqlSession2 = sessionFactory.openSession();
    }

    /**
     *          Mybatis 二级缓存
     *              1.默认开启
     *              2.作用域  是 SqlSessionFactory.
     *              要把对象 存到 二级缓存 需要
     *                      1. 对象要实现 可序列化接口
     *                      2. 对应的 mapper中 必须添加 cache节点。
     * 
     * 
     */
    @Test
    public void test01() throws InterruptedException {
        CityMapper cm1 = sqlSession1.getMapper(CityMapper.class);
        CityMapper cm2= sqlSession2.getMapper(CityMapper.class);
        System.out.println(cm1.selectCityById(1));
        sqlSession1.commit();
        Thread.sleep(5000);
        System.out.println("-----");
        System.out.println(cm2.selectCityById(1));
        Thread.sleep(3000);
        System.out.println(cm2.selectCityById(1));
    }

    /**
     *  二级缓存 失效的情况:
     *      1 同一个 命名空间下 执行了 增删改(提交)。（不关心是哪个连接执行的）
     *      2 手动设置 某个查询 单独不使用 二级缓存
     *
     */

    @Test
    public void test02(){
        CityMapper cm1 = sqlSession1.getMapper(CityMapper.class);
        CityMapper cm2= sqlSession2.getMapper(CityMapper.class);
        System.out.println(cm1.selectCityById(1));
        sqlSession1.commit();

        cm1.insertCity("茂名",3);
        sqlSession1.commit();// cityMapper 2级失效

        System.out.println("-----");
        System.out.println(cm2.selectCityById(1));
    }


    @Test
    public void test03(){
        CityMapper cm1 = sqlSession1.getMapper(CityMapper.class);
        CityMapper cm2= sqlSession2.getMapper(CityMapper.class);
        System.out.println(cm1.selectCityById(1));
//        sqlSession1.commit();

        System.out.println("-----");
        System.out.println(cm1.selectCityById(1));
    }

    @Test
    public void test04(){
        CityMapper cm1 = sqlSession1.getMapper(CityMapper.class);
        ProvinceMapper pm1 = sqlSession1.getMapper(ProvinceMapper.class);
        CityMapper cm2= sqlSession2.getMapper(CityMapper.class);

        System.out.println(cm1.selectCityById(1));
        sqlSession1.commit();
        pm1.insertProvince("海南2");
        sqlSession1.commit();

        System.out.println("-----");
        System.out.println(cm2.selectCityById(1));

    }
}
